<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./dist/css/layui.css">
    <script src="./dist/layui.js"></script>
</head>

<body>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked">获取选中节点数据</button>
        <button type="button" class="layui-btn layui-btn-sm" lay-demo="setChecked">勾选指定节点</button>
        <button type="button" class="layui-btn layui-btn-sm" lay-demo="reload">重载实例</button>
    </div>
    <div id="test12" class="demo-tree-more"></div>
</body>

</html>
<script>
    layui.use(['tree', 'util', 'jquery'], async function () {
        var tree = layui.tree
            , layer = layui.layer
            , util = layui.util
            , $ = layui.jquery



        //按钮事件
        util.event('lay-demo', {
            getChecked: function (othis) {
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

                layer.alert(JSON.stringify(checkedData), { shade: 0 });
                console.log(checkedData);
            }
            , setChecked: function () {
                tree.setChecked('demoId1', ['11100000000']); //勾选指定节点
            }
            , reload: function () {
                //重载实例
                tree.reload('demoId1', {

                });

            }
        });
        let data = await getHttp(id)
        //基本演示
        tree.render({
            elem: '#test12'
            , data: data
            , showCheckbox: true
            , id: 'demoId1'
            , isJump: true
            , lazyLoad: true //是否开启懒加载
            , onlyIconControl: true
            , dbClick:true //是否开启双击触发节点点击事件
            , lazyLoadData: (id) => {
                return new Promise((resolve) => {
                    var settings = {
                        "url": "http://localhost:7971/devManage/querySwjg",
                        "method": "POST",
                        "timeout": 0,
                        "headers": {
                            "Content-Type": "application/json"
                        },
                        "data": JSON.stringify({
                            "swjgDm": id
                        }),
                    };
                    $.ajax(settings).done(function ({ data }) {
                        resolve(data)
                    });
                })
            } //懒加载的异步请求,返回Promise
            , accordion: true
            , click: function (obj) {
                var data = obj.data;  //获取当前点击的节点数据
                layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
            }
        });

        function getHttp() {
            return new Promise((resolve) => {
                var settings = {
                    "url": "XXXXX",
                    "method": "POST",
                    "timeout": 0,
                    "headers": {
                        "Content-Type": "application/json"
                    },
                    "data": JSON.stringify({}),
                };

                $.ajax(settings).done(function ({ data }) {
                    resolve(data)
                });
            })

        }
    })

</script>